﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using DevExpress.Data;
using ProgramMain.Database;
using ProgramMain.Google;

namespace ProgramMain.OpticNet
{
    public partial class VertexDetail : ProgramMain.Framework.BaseModalDetailCtl
    {
        public VertexDetail()
        {
            InitializeComponent();
        }

        public string Street { get; set; }

        private string _likeSreet()
        {
            string res = "";
            try
            {
                string[] mas = Regex.Split(Street.Trim(), "\\s+");
                foreach (string s in mas)
                {
                    if (s != s.ToLower())
                        res += ("%" + s.Trim());
                }
                res += "%";
            }
            catch (Exception)
            {

                res = null;
            }

            return res;
        }
        public string SearchAdres(Coordinate coordinate, out string nameObject)
        {
            nameObject = "";
            string res = "";
            GoogleSearch.GoogleSearch search = new GoogleSearch.GoogleSearch(coordinate);
            List<GoogleSearch.SearchResult> results = search.Search();
            if (results.Count > 0)
            {
                string[] addressComponents = (from q in results[0].AddressComponents where q.Types.First() == "route" select q.LongName).ToArray();
                if (addressComponents.Length > 0)
                    res += addressComponents[0];
                if (!string.IsNullOrEmpty(addressComponents[0])) Street = addressComponents[0];

                addressComponents = (from q in results[0].AddressComponents where q.Types.First() == "street_number" select q.LongName).ToArray();
                if (addressComponents.Length > 0)
                    res += " " + addressComponents[0];

                addressComponents = (from q in results[0].AddressComponents where q.Types.First() == "point_of_interest" select q.LongName).ToArray();
                if (addressComponents.Length > 0)
                    nameObject = addressComponents[0];
            }
            return res.Trim();
        }

       
        protected override void InitializeDataControls()
        {
            this.vertexTypesTableAdapter.SelectCommandTimeout = ProgramMain.Properties.Settings.Default.LongQueryTimeout;
            this.vertexTypesTableAdapter.Fill(this.simpleOpticNetDB.VertexTypes, null);

            this.ownerCompaniesTableAdapter.SelectCommandTimeout = ProgramMain.Properties.Settings.Default.LongQueryTimeout;
            this.ownerCompaniesTableAdapter.Fill(this.simpleOpticNetDB.OwnerCompanies,null);
            
            this.txtEditLongitude.Properties.ReadOnly = ShowMode != ShowModeTypes.Edit;
            this.txtEditLatitude.Properties.ReadOnly = ShowMode != ShowModeTypes.Edit;

            if (ShowMode == ShowModeTypes.Add)
            try
            {
                if (CurrentRowView == null) return;
                SimpleOpticNetDB.VertexesRow row = (SimpleOpticNetDB.VertexesRow)(CurrentRowView).Row;
                if (this.vertexTypesBindingSource.Count > 0)
                {
                    this.vertexTypesBindingSource.MoveFirst();
                    SimpleOpticNetDB.VertexTypesRow vertexTypesRow = (SimpleOpticNetDB.VertexTypesRow)((DataRowView)this.vertexTypesBindingSource.Current).Row;
                   if (vertexTypesRow !=null)
                   {
                       row.VertexTypeID = vertexTypesRow.ID;
                       row.VertexType = vertexTypesRow.Caption;
                   }
                }
                if (this.ownerCompaniesBindingSource.Count > 0)
                {
                    for (int i = 0; i < ownerCompaniesBindingSource.Count; i++)
                    {
                        ownerCompaniesBindingSource.Position = i;
                        SimpleOpticNetDB.OwnerCompaniesRow ownerCompaniesRow =
                            (SimpleOpticNetDB.OwnerCompaniesRow)
                            ((DataRowView) this.ownerCompaniesBindingSource.Current).Row;
                        if (ownerCompaniesRow != null && ownerCompaniesRow.IsDefault)
                        {
                            row.OwnerCompanyID = ownerCompaniesRow.ID;
                            row.OwnerCompany = ownerCompaniesRow.Caption;
                        }
                    }
                }

                string name = string.Empty;
                row.Address = SearchAdres(new Coordinate(row.Longitude,row.Latitude), out name);
                row.Caption = name;
            }
            catch(Exception)
            {}

            this.vTechObjectsTableAdapter.SelectCommandTimeout = ProgramMain.Properties.Settings.Default.LongQueryTimeout;
            this.vTechObjectsTableAdapter.ClearBeforeFill = true;
            this.vTechObjectsTableAdapter.Fill(this.simpleOpticNetDB.vTechObjects, _likeSreet());

             
        }

        protected override void BindDataControls()
        {
            this.lookupVertexTypeID.DataBindings.Add(new Binding("EditValue", this.dataSource, "VertexTypeID"));
            this.lookupOwnerCompanyID.DataBindings.Add(new Binding("EditValue", this.dataSource, "OwnerCompanyID"));
            this.lookTechObjectsID.DataBindings.Add(new Binding("EditValue", this.dataSource, "TechObjectID"));
            this.txtEditCaption.DataBindings.Add(new Binding("Text", this.dataSource, "Caption"));
            this.txtEditNumber.DataBindings.Add(new Binding("Text", this.dataSource, "Number"));
            this.txtEditAddress.DataBindings.Add(new Binding("Text", this.dataSource, "Address"));
            this.memoEditDesc.DataBindings.Add(new Binding("Text", this.dataSource, "Description"));
            this.txtEditLongitude.DataBindings.Add(new Binding("Text", this.dataSource, "Longitude"));
            this.txtEditLatitude.DataBindings.Add(new Binding("Text", this.dataSource, "Latitude"));
            this.txtLength.DataBindings.Add(new Binding("Text", this.dataSource, "Length"));
           
             if (ShowMode == ShowModeTypes.Edit)
             {
                 Street = this.txtEditAddress.Text;
             }
            lookTechObjectsID.Properties.PopupFormSize = new Size(400, 500);
            lookTechObjectsID.Properties.DropDownRows = 20;
            lookTechObjectsID.Properties.Columns[0].SortOrder = ColumnSortOrder.Ascending;
        }

        private void lookTechObjectsID_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (e.Button.Tag != null && e.Button.Tag.ToString() == "all")
            {
                this.vTechObjectsTableAdapter.SelectCommandTimeout = ProgramMain.Properties.Settings.Default.LongQueryTimeout;
                this.vTechObjectsTableAdapter.ClearBeforeFill = true;
                this.vTechObjectsTableAdapter.Fill(this.simpleOpticNetDB.vTechObjects, null);
            }
        }
    }
}
